import Taro from '@tarojs/taro';
import ENV_TYPE = Taro.ENV_TYPE;
import {AndroidStorage} from "./AndroidStorage";
import {WebStorage} from "./WebStorage";
import {IosStorage} from "./IosStorage";
import {WeappStorage} from "./WeappStorage";

export interface MyStorage {
  set: (key: string, value: string) => void
  get: (key: string) => string
}

let storage: MyStorage | null = null

export function getStorage(): MyStorage{
  if(!storage){
    storage = newStorage()
  }
  return storage
}

function newStorage(){
  if(Taro.getEnv() === ENV_TYPE.WEB){
    try {
      return new AndroidStorage()
    }catch{
      console.log("创建AndroidStorage失败")
    }
    try {
      return new IosStorage()
    }catch{
      console.log("创建IosStorage失败")
    }
    try {
      return new WebStorage()
    }catch {
      console.log("创建WebStorage失败")
    }
  }else if(Taro.getEnv() === ENV_TYPE.WEAPP){
    return new WeappStorage()
  }
  throw new Error("还未实现当前环境的MyStorage")
}
